﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AssessmentTool.PresentationLogic;
using AssessmentTool.PresentationLogic.EventArguments;

namespace AssessmentTool
{
	public partial class Help : UserControl
	{
#region (DependencyProperty)
        public FrameworkElement SetContent
        {
            get { return (FrameworkElement)GetValue(SetContentProperty); }
            set { SetValue(SetContentProperty, value); }
        }

        public static readonly DependencyProperty SetContentProperty =
            DependencyProperty.Register("SetContent", typeof(FrameworkElement), typeof(Help), new PropertyMetadata(null, new PropertyChangedCallback(OnSetContentChanged)));
        private static void OnSetContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Help)d).OnSetContentChanged(e);
        }
        protected virtual void OnSetContentChanged(DependencyPropertyChangedEventArgs e)
        {

            if (SetContent != null)
                BoxContent.Content = SetContent;
        }
        public String WindowWidth
        {
            get { return (String)GetValue(WindowWidthProperty); }
            set { SetValue(WindowWidthProperty, value); }
        }
        public static readonly DependencyProperty WindowWidthProperty =
             DependencyProperty.Register("WindowWidth", typeof(String), typeof(Help), new PropertyMetadata(null, new PropertyChangedCallback(propertyChangedCallback)));

        public String WindowHeight
        {
            get { return (String)GetValue(WindowHeightProperty); }
            set { SetValue(WindowHeightProperty, value); }
        }
        public static readonly DependencyProperty WindowHeightProperty =
            DependencyProperty.Register("WindowHeight", typeof(String), typeof(Help), new PropertyMetadata(null, new PropertyChangedCallback(propertyChangedCallback)));
      
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Help)d).propertyChangedCallback(e);
        }
        protected virtual void propertyChangedCallback(DependencyPropertyChangedEventArgs e)
        {

            if (SetContent != null)
                BoxContent.Content = SetContent;
        }
#endregion
        bool modal = false;
        public Help()
		{
			// Required to initialize variables
			InitializeComponent();
            EventDispatcher.getInstance().subscrive("CLOSEHELP",onClose);
            EventDispatcher.getInstance().subscrive("CLOSEMODAL", onCloseModal);
            EventDispatcher.getInstance().subscrive("SHOWHELP", showModal);
            
            
		}
        public void onClose(EventArgs e)
        {
           
            Visibility = Visibility.Collapsed;
        }
        public void onCloseModal(EventArgs e)
        { 
            if(modal)
                onClose(e);
        }
        
        public void showModal(EventArgs e)
        {
            if ((this.Visibility == Visibility.Visible) && !modal)
            {
                return;
            }
            this.Visibility = Visibility.Visible;
            ShowHelpEventArgs ev = (ShowHelpEventArgs)e;
            modal = ev.ShowModal;
            WindowWidth = ev.Width + "";
            WindowHeight = ev.Height + "";

            TextBlock tb = null;
            ScrollViewer view = null;
            if(!String.IsNullOrEmpty(ev.Text))
            {
                view = new ScrollViewer();
                view.HorizontalScrollBarVisibility=ScrollBarVisibility.Disabled;
                view.VerticalScrollBarVisibility=ScrollBarVisibility.Auto;
                
                
                
                tb = new TextBlock();
                tb.Style =(Style) App.Current.Resources["NormalText"];
                tb.TextWrapping = TextWrapping.Wrap;
                
                String[] strings=ev.Text.Split('$');
                if (!modal)
                {
                    tb.Inlines.Add(new Run() { Text = ev.Titolo });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new LineBreak());
                }
                foreach (String s in strings)
                {
                    tb.Inlines.Add(new Run() { Text = "• " + s });
                    tb.Inlines.Add(new LineBreak());
                    tb.Inlines.Add(new LineBreak());
                }
                tb.VerticalAlignment = VerticalAlignment.Stretch;
                view.Content = tb;
                SetContent = view;
            }
            if (ev.ShowModal)
            {
                tb.FontSize = 10;
                LayoutRoot.Width = ev.Width;
                WindowHeight = tb.Height + "";
                this.HorizontalAlignment = HorizontalAlignment.Left;
                this.VerticalAlignment = VerticalAlignment.Top;
                this.Margin = ev.Margin;
                closeButton.Visibility = Visibility.Collapsed;
                LayoutRoot.MaxHeight = ev.Height;
                view.MaxHeight = ev.Height;
                modal = true;
                
            }
            else
            {
                tb.FontSize = 12;
                modal = false;
                WindowHeight = tb.Height + "";
                this.HorizontalAlignment = HorizontalAlignment.Center;
                this.VerticalAlignment = VerticalAlignment.Center;
                this.Margin = new Thickness(0, 0, 0, 0);
                LayoutRoot.Width = double.NaN;
                LayoutRoot.MaxHeight = ev.Height;
                closeButton.Visibility = Visibility.Visible;
                view.MaxHeight = ev.Height;
            }
            Titolo.Text = "Qualche informazione in più..";
            Titolo.TextWrapping = TextWrapping.Wrap;
        }
	}
}